package vg.services.user_interface_manager.additional_swing_components.conditionPanel.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;

import vg.services.user_interface_manager.additional_swing_components.Bracket;
import vg.services.user_interface_manager.additional_swing_components.ValueComboBox;

public class GeneralCondition implements ICondition {
	// Data
	private Map<String, Set<String>> general_attributes;
	// Swing data
	private JComboBox left_part; // left part of conditional
	private ValueComboBox right_part; // right part of conditional                                        
	private JButton close_button; // button for closing (deleting) of condition
	private Bracket left_bracket;
	private Bracket right_bracket;
	private JPanel view;
	private JComboBox type;
	
	public GeneralCondition(Map<String, Set<String>> generalAttributes, String[] types) {
		// initialize
		general_attributes = generalAttributes;
		view = new JPanel();
		
		// initialize left part
		String[] a_name = new String[generalAttributes.size()];
		a_name = generalAttributes.keySet().toArray(a_name);
		// sort names for better usability 
		Arrays.sort(a_name);
		left_part = new JComboBox(a_name);
		String selected_name = (String)left_part.getSelectedItem();
		
		// initialize right part
		if(selected_name != null) {
			Set<String>value = general_attributes.get(selected_name);
			String[] a_value = new String[value.size()];
			a_value = value.toArray(a_value);
			Arrays.sort(a_value);
			right_part = new ValueComboBox(a_value);			
		} else {
			right_part = new ValueComboBox();
		}
		
		left_part.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String selected_name = (String)left_part.getSelectedItem();
				
				// initialize right part
				if(selected_name != null) {
					Set<String>value = general_attributes.get(selected_name);
					String[] a_value = new String[value.size()];
					a_value = value.toArray(a_value);
					Arrays.sort(a_value);
					right_part.removeAllItems();
					right_part.addItems(a_value);
				}			
			}
		});
		
		// initialize close button
		// TODO: need change text to picture
		close_button = new JButton("Close");
		
		// initialize brackets
		left_bracket = new Bracket(Bracket.DEF_LEFT_BRACKET);
		right_bracket = new Bracket(Bracket.DEF_RIGHT_BRACKET);

		// initialize type choice 
		type = new JComboBox(types);
		
		// build graphical component
		view.setLayout(new GridBagLayout());
		GridBagConstraints gbc = null;
		gbc = new GridBagConstraints(0,0,  1,1,  0,0,  GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0);
		view.add(type, gbc);
		gbc = new GridBagConstraints(3,0,  1,1,  0,0,  GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0);
		view.add(close_button, gbc);

		gbc = new GridBagConstraints(0,1,  1,1,  0,0,  GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0);
		view.add(left_bracket.getView(), gbc);
		gbc = new GridBagConstraints(1,1,  1,1,  1,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),  0,0);
		view.add(left_part, gbc);
		gbc = new GridBagConstraints(2,1,  1,1,  1,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),  0,0);
		view.add(right_part.getView(), gbc);
		gbc = new GridBagConstraints(3,1,  1,1,  0,0,  GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0);
		view.add(right_bracket.getView(), gbc);
	}
//=============================================================================
//-------------------GET METHODS-----------------------------------------------
	public String getName() {
		Object name = left_part.getSelectedItem();
		if(name == null)
			 return "";
		return name.toString();
	}
	
	public String getValue() {
		Object value = right_part.getSelectedItem();
		if(value == null)
			return "";
		return value.toString();
	}
	
	public String getLeftBracket() {
		return left_bracket.getText();
	}
	
	public String getRightBracket() {
		return right_bracket.getText();
	}
	
	@Override
	public JComponent getView() {
		return view;
	}
	
	@Override
	public void updateUITheme() {
		view.updateUI();
	}
	
	@Override
	public void addChangeTypeListener(ActionListener l) {
		type.addActionListener(l);
	}
	
	@Override
	public void removeChangeTypeListener(ActionListener l) {
		type.removeActionListener(l);
	}

	@Override
	public void addClosingListener(ActionListener l) {
		close_button.addActionListener(l);
	}	
	
	@Override
	public void removeClosingListener(ActionListener l) {
		close_button.removeActionListener(l);
	}
}
